<?php


namespace App\Domain\HealthCheck\Services;

use App\Domain\FamilyMember\Models\FamilyMemberModel;
use App\Domain\HealthCheck\Dao\HealthCheckDao;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;
use App\Exceptions\BadRequestException;
use App\Domain\HealthCheck\Models\HealthCheckModel;

class HealthCheckService
{
    //保存检测数据
    public function create($param)
    {
        $healthDao = new HealthCheckDao();
        $res = $healthDao->create([
            'uid' => $param['memberUid'],
            'type' => $param['type'],
            'item' => $param['item'],
            'value0' => $param['value0'],
            'value1' => $param['value1'],
            'value2' => $param['value2'],
        ]);
        return $res;
    }

    /**
     * 测量数据列表
     * @param $params
     * @return LengthAwarePaginator|Builder[]|Collection
     */
    public function list($params)
    {
        $query = HealthCheckModel::query()
            ->orderBy('id', 'desc')
            ->select('health_check.*')
            ->where('health_check.state', 1);
        $query = $query->where(function (Builder $query) use ($params) {
            if (isset($params['uid']))
                $query->where('uid', $params['uid']);
            if (isset($params['type']))
                $query->where('type', $params['type']);
            if (isset($params['start_time']))
                $query->where('create_time', '>=', $params['start_time']);
            if (isset($params['end_time']))
                $query->where('create_time', '<=', $params['end_time']);
        });
        if (isset($params['pageSize']) && isset($params['currentPage'])) {
            return $query->paginate($params['pageSize'], ['*'], 'page', $params['currentPage']);
        } else {
            return $query->get();
        }
    }

    public function chart($params)
    {
        $query = HealthCheckModel::query()
            ->orderBy('id', 'desc')
            ->select('health_check.*')
            ->where('health_check.state', 1);
        $query = $query->where(function (Builder $query) use ($params) {
            if (isset($params['uid']))
                $query->where('uid', $params['uid']);
            if (isset($params['type']))
                $query->where('type', $params['type']);
            if (isset($params['startDate']))
                $query->where('create_time', '>=', $params['startDate']);
            if (isset($params['endDate']))
                $query->where('create_time', '<=', $params['endDate']);
        });
        return $query->get();
    }


    /**
     * 测量数据列表
     * @param $params
     * @return LengthAwarePaginator|Builder[]|Collection
     */
    public function pclist($params)
    {

        $query = HealthCheckModel::query()
            ->orderBy('id', 'desc')
            ->select(['health_check.*', 'family_member.name', 'family_member.idCard'])
            ->leftJoin('family_member', function ($join) {
                $join->on('family_member.id', '=', 'health_check.uid');
            })
            ->where('health_check.state', 1);
        $query = $query->where(function (Builder $query) use ($params) {
            if (isset($params['name']))
                $query->where('family_member.name', '=', $params['name']);
            if (isset($params['idcard']))
                $query->where('family_member.idCard', '=', $params['idcard']);
            if (isset($params['type']))
                $query->where('type', $params['type']);
            if (isset($params['id']))
                $query->where('health_check.id', $params['id']);
            if (isset($params['start_time']))
                $query->where('create_time', '>=', $params['start_time']);
            if (isset($params['end_time']))
                $query->where('create_time', '<=', $params['end_time']);
        });
        if (isset($params['pageSize']) && isset($params['currentPage'])) {
            return $query->paginate($params['pageSize'], ['*'], 'page', $params['currentPage']);
        } else {
            return $query->get();
        }
    }
}
